package dao.impl;

import bean.pojo.Products;
import commons.dbHelper.DBHelper;
import dao.ProductDao;

import java.util.ArrayList;
import java.util.List;

/**
 * dao.impl
 * User: 李雨行
 * Date: 2024/12/17 15:54
 * motto:   沉淀哥
 * Description:
 * Version: V1.0
 */
public class ProductDaoImpl implements ProductDao {
    DBHelper dbHelper=new DBHelper();
    @Override
    public List<Products> findByPage(Integer productId, String productName, String productCategory,Integer page,Integer limit) {
        List<Object> list =new ArrayList<>();
        StringBuffer sql=new StringBuffer("select product_id,product_name,price,stock,product_description,product_category" +
                " from product where 1=1 ");
        if (productId!=null){
            sql.append(" and product_id=? ");
            list.add(productId);
        }
        if (productName!=null){
            sql.append(" and product_name like concat('%',?,'%') ");
            list.add(productName);
        }
        if (productCategory!=null){
            sql.append(" and product_category like concat('%',?,'%')");
            list.add(productCategory);
        }
        sql.append(" order by product_id desc limit "+(page-1)*limit+" , "+limit);
        System.out.println("sql:"+sql);
        return dbHelper.getBeanList(Products.class,sql.toString(),list.toArray());
    }

    @Override
    public boolean add(Products products) {
        String sql="insert product (product_name,price,stock,product_description,product_category) values(?,?,?,?,?)";
        return dbHelper.update(sql,products.getProductName(),products.getPrice(),products.getStock(),products.getProductDescription(),products.getProductCategory())>0;
    }

    @Override
    public boolean deleteById(Integer id) {
        String sql="delete from product where product_id=?";
        return dbHelper.update(sql,id)>0;
    }

    @Override
    public boolean update(Products products) {
        String sql="update product set product_name=?,price=?,stock=?,product_description=?,product_category=? where product_id=?";
        return dbHelper.update(sql,products.getProductName(),products.getPrice(),products.getStock(),products.getProductDescription(),products.getProductCategory(),products.getProductId())>0;
    }

    @Override
    public Integer maxPage(Integer productId,String productName,String productCategory) {
        List<Object> list =new ArrayList<>();
        StringBuffer sql=new StringBuffer("select product_id,product_name,price,stock,product_description,product_category" +
                " from product where 1=1 ");
        if (productId!=null){
            sql.append(" and product_id=? ");
            list.add(productId);
        }
        if (productName!=null){
            sql.append(" and product_name like concat('%',?,'%') ");
            list.add(productName);
        }
        if (productCategory!=null){
            sql.append(" and product_category like concat('%',?,'%')");
            list.add(productCategory);
        }
        System.out.println("sql:"+sql);
        return dbHelper.getBeanList(Products.class,sql.toString(),list.toArray()).size();
    }
}
